package main

import (
	"flag"
	"fmt"

	"e4ting/pb/e4ting"
	"e4ting/internal/config"
	"e4ting/internal/server"
	"e4ting/internal/svc"

	"e4ting/internal/cron"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/go-zero/gateway"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)

var configFile = flag.String("f", "etc/e4ting.yaml", "the config file")
var configGate = flag.String("g", "etc/gateway.yaml", "RestFull API 代理")

func main_gateway() {
	var c gateway.GatewayConf
    conf.MustLoad(*configGate, &c)
    gw := gateway.MustNewServer(c)
    defer gw.Stop()
    gw.Start()
}

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)

	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		e4ting.RegisterE4TingServer(grpcServer, server.NewE4tingServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	defer s.Stop()

	_ = consul.RegisterService(c.ListenOn, c.Consul)

	cron.Start(c.Crontab, ctx)
	go main_gateway()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}
